package com.axend.aerosense.login.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.ISimpleAction;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.base.viewmodel.SimpleModelFactroy;
import com.axend.aerosense.common.base.IHostLayout;
import com.axend.aerosense.common.base.IToolBar;
import com.axend.aerosense.common.bean.UserInfo;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.login.BR;
import com.axend.aerosense.login.BuildConfig;
import com.axend.aerosense.login.R;
import com.axend.aerosense.login.activity.LoginIpSettingsActivity;
import com.axend.aerosense.login.databinding.LoginEnLoginFragmentBinding;
import com.axend.aerosense.login.utils.TextViewUtil;
import com.axend.aerosense.login.viewmodel.LoginUserInfoViewModel;
import com.blankj.utilcode.util.ToastUtils;

public class LoginEnLoginFragment
        extends MvvmBaseFragment<LoginEnLoginFragmentBinding, LoginUserInfoViewModel>
        implements ISimpleAction<UserInfo> {

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }


    private void initView() {
        viewDataBinding.loginInfoView.loginPasswordMsg.setVisibility(View.GONE);
        viewDataBinding.loginInfoView.loginRegView.setVisibility(View.GONE);
        try {
            TextViewUtil.initText(viewDataBinding.loginInfoView.loginUserAgreement);
        } catch (Exception e) {
            e.printStackTrace();
        }

        viewDataBinding.loginInfoView.loginButton.setText(R.string.login_login);
        viewDataBinding.loginInfoView.loginButton.setOnClickListener(v->{
            if(!viewModel.isChecked.getValue()){
                ToastUtils.showShort(R.string.common_user_agreement_tips);
                return;
            }
            viewModel.onLogin(getActivity());
        });

        viewDataBinding.loginSignUp.setOnClickListener(v->{
            FragmentUtil.add(getActivity(), (IHostLayout) getActivity(), LoginEnRegFragment.newInstance(), this, true);
        });

        viewDataBinding.loginInfoView.loginForgotPassword.setOnClickListener(v->{
            FragmentUtil.add(getActivity(), (IHostLayout) getActivity(), LoginResetPasswordFragment.newInstance(), this, true);
        });

        viewDataBinding.loginInfoView.loginPasswordEye.setOnClickListener(v -> {
            Boolean isEyeOpen = viewModel.isEyeOpen.getValue();
            int pos = viewDataBinding.loginInfoView.loginPasswordTv.getSelectionStart();
            if (!isEyeOpen){
                viewDataBinding.loginInfoView.loginPasswordTv.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }else {
                viewDataBinding.loginInfoView.loginPasswordTv.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
            viewModel.changeEye();
            viewDataBinding.loginInfoView.loginPasswordTv.setSelection(pos);
        });

        viewDataBinding.loginInfoView.loginCheckbox.setOnClickListener(v -> viewModel.changeCheck());

        if (BuildConfig.DEBUG) {
            viewDataBinding.loginTvServerSettings.setVisibility(View.VISIBLE);
            viewDataBinding.loginTvServerSettings.setOnClickListener(v -> {
                Intent intent = new Intent(getActivity(), LoginIpSettingsActivity.class);
                getActivity().startActivity(intent);
            });
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    @Override
    public int getLayoutId() {
        return R.layout.login_en_login_fragment;
    }

    @Override
    public int getBindingVariable() {
        return BR.viewmdel;
    }

    @Override
    protected LoginUserInfoViewModel getViewModel() {
        SimpleModelFactroy<UserInfo> factroy = new SimpleModelFactroy<UserInfo>(this);
        String account = MmkvHelper.getInstance().getMmkv().getString(Gobal.SAVE_KEY.LOGIN_EMAIL, "");
        LoginUserInfoViewModel tmp = new ViewModelProvider(this, factroy).get(LoginUserInfoViewModel.class);
        tmp.userEmail.setValue(account);
        return tmp;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    public void onResume() {
        super.onResume();
        ((IToolBar)getActivity()).setToolBarVisibility(View.GONE);
    }

    @Override
    public void onSuccess(UserInfo userInfo) {
        getActivity().finish();
    }

    @Override
    public void onFail(String decs) {

    }
}
